package com.nananer7.servlet;

import com.nananer7.dao.IdeaDao;
import com.nananer7.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ShareServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();

        //得到表单提交的内容
        String username = request.getParameter("username");
        String subject = request.getParameter("subject");
        String categories = request.getParameter("categories");
        String description = request.getParameter("description");

        int uid=0;
        if(username == null){
            session.setAttribute("msg","您还没有登录，无法使用分享功能，请先去登录");
            request.getRequestDispatcher("/share-ideas.jsp").forward(request, response);
            return;
        }else if(subject == null){
            session.setAttribute("msg","subject不能为空！");
            request.getRequestDispatcher("/share-ideas.jsp").forward(request, response);
            return;

        }else{
            uid=new UserDao().findUidByUsername(username);
            boolean bl =new IdeaDao().addIdea(uid,subject,categories,description);
            request.getRequestDispatcher("/index.jsp").forward(request, response);

        }
    }
}
